/*
 * @Descripttion:
 * @version:
 * @Author: kl2018
 * @Date: 2020-08-04 11:06:39
 * @LastEditors: kl2018
 * @LastEditTime: 2020-08-10 10:04:26
 */
import React from 'react';
import loadjs from 'loadjs';
import { inject, observer } from 'mobx-react';
import {workflowPublicFiles} from "../../constants/syncFiles";

@inject('odocRequestStore', 'odocCompsStore')
@observer
class Main extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      hasLoad: false,
    };

    this.changeRemarkInfo = this.changeRemarkInfo.bind(this);
  }

  componentDidMount() {
    const init = () => {
      this.setState({ hasLoad: true });

      this.updateRemark();
    };
    loadjs.isDefined('workflowPublic') ? loadjs.ready('workflowPublic', init) : loadjs(workflowPublicFiles, 'workflowPublic', init);
  }

  updateRemark() {
    const { visible } = this.props;
    if (visible) { // 流转页面显示才需要更新签字意见框的值
      // 签字意见显示在表单中时，如果有值，那么就取这个值
      const remark = WfForm.getFieldValue('field-4');
      if (window.CK_REF && window.CK_REF.oDocRemark && window.CK_REF.oDocRemark.getEditor()) {
        if (remark) {
          window.CK_REF.oDocRemark.getEditor().setData(remark);
        }
      } else {
        this.updateRemarkFn = window.setTimeout(() => {
          this.updateRemark();
        }, 300);
      }
    }
  }

  changeRemarkInfo = (params = {}) => {
    const { odocCompsStore } = this.props;
    const { workflowStores } = odocCompsStore;
    const { globalStore } = workflowStores;
    const { changeSubmitParam } = globalStore;
    changeSubmitParam(params);
  }

  componentDidUpdate() {
    this.updateRemark();
  }

  render() {
    const { hasLoad } = this.state;

    if (hasLoad) {
      const { WfSignInput } = window.weaWorkflowPublic;
      const { odocCompsStore } = this.props;
      const { workflowStores } = odocCompsStore;
      const { globalStore } = workflowStores;
      const { commonParam, markInfo = {}, loadMarkInfo } = globalStore;
      const { requestid, requestType } = commonParam;
      const { remarkInfo = {} } = markInfo;

      return (<WfSignInput ecId={`${this && this.props && this.props.ecId || ''}_WfSignInput@ootaq3`}
        ref={'oDocRemarkCK'}
        markInfo={markInfo}
        requestType={requestType}
        requestid={requestid}
        isShowSignInput={true}
        editorId={'oDocRemark'}
        maxHeight={150}
        {...remarkInfo}
        changeRemarkInfo={this.changeRemarkInfo}
        saveCallBack={loadMarkInfo}
      />);
    }
    return <div />;
  }
}

export default Main;
